🔶章節:
🔹[開頭]
🔹[常用的15個數字處理函式]
🔹[常用的10個陣列函式]
🔹[常用的5個添加&刪除函式]
🔹[總結]
如果影片中不清楚,需要補充的地方我會再添加到這邊~
👆教學中的[練習]程式碼一併附上,影片中會有每組的講解、說明更清楚👆
(今天影片長度也是19分鐘...因為包含了超多的函式介紹與說明、範例)
(影片中,聲音啞啞的還請見諒😞😞喝水康復中)
abs(x)
$number = -5;
$absoluteValue = abs($number);
// $absoluteValue = 5
round($number, $precision)
$number = 3.7;
$roundedNumber = round($number);
// $roundedNumber = 4
floor($number)
$number = 3.7;
$floorNumber = floor($number);
// $floorNumber = 3
$number = -4.2;
$floorNumber = floor($number);
// $floorNumber = -5
ceil($number)
$number = 3.2;
$ceiledNumber = ceil($number);
// $ceiledNumber = 4
$number = -4.7;
$ceiledNumber = ceil($number);
// $ceiledNumber = -4
sqrt($number)
$number = 16;
$squareRoot = sqrt($number);
// $squareRoot = 4
pow($base, $exponent)
$base = 2;
$exponent = 3;
$result = pow($base, $exponent);
// $result = 8
min($number1, $number2, ..., $numberN)
$number1 = 10;
$number2 = 5;
$number3 = 8;
$minValue = min($number1, $number2, $number3);
// $minValue = 5
$numbers = [7, 2, 9, 1, 4];
$minValue = min(...$numbers);
// $minValue = 1
使用 ... 運算子可以將陣列拆解為個別的參數。
max($number1, $number2, ..., $numberN)
$number1 = 10;
$number2 = 5;
$number3 = 8;
$maxValue = max($number1, $number2, $number3);
// $maxValue = 10
$numbers = [7, 2, 9, 1, 4];
$maxValue = max(...$numbers);
// $maxValue = 9
rand($min, $max)
$randomNumber = rand(1, 100); // 生成1到100之間的隨機整數
生成一個範圍內的隨機整數,使用 Mersenne Twister 算法。
mt_rand($min, $max)
$randomNumber = mt_rand(10, 20); // 生成10到20之間的隨機整數
生成一個範圍內的隨機整數,使用 Mersenne Twister 算法。
number_format($number, $decimals, $decimalSeparator, $thousandsSeparator)
$number = 1234567.89;
$formattedNumber = number_format($number);
// $formattedNumber = "1,234,567"
$number = 1234.5678;
$formattedNumber = number_format($number, 2, ".", " ");
// $formattedNumber = "1 234.57"
intval($string, $base)
(下方)使用 intval() 函式將字串 "123" 轉換為整數 123。
$numberString = "123";
$integerValue = intval($numberString);
// $integerValue = 123
(下方)我們指定進位制為 16,並將十六進制字串 "1A" 轉換為整數,得到的結果為 26。
$hexadecimalString = "1A";
$integerValue = intval($hexadecimalString, 16);
// $integerValue = 26
floatval($var)
將字串 "3.14" 轉換為浮點數 3.14。
$floatString = "3.14";
$floatValue = floatval($floatString);
// $floatValue = 3.14
$integerValue = 42;
$floatValue = floatval($integerValue);
// $floatValue = 42.0
fmod($dividend, $divisor)
$dividend = 10.5;
$divisor = 3;
$remainder = fmod($dividend, $divisor);
// $remainder = 1.5
// 10.5 除以 3 等於 3 餘 1.5
is_numeric($var)
$number1 = 42;
$isNumeric1 = is_numeric($number1); // $isNumeric1 = true
$number2 = "3.14";
$isNumeric2 = is_numeric($number2); // $isNumeric2 = true
$string = "Hello";
$isNumeric3 = is_numeric($string); // $isNumeric3 = false
array(value1, value2, ...)
$numbers = array(1, 2, 3, 4, 5);
這種寫法也是用來創建陣列,但與 array() 函式的效果相同。
$fruits = ["apple", "banana", "cherry"];
count($array, $mode)
$numbers = [1, 2, 3, 4, 5];
$count = count($numbers);
// $count = 5
in_array($needle, $haystack, $strict)
$fruits = ["apple", "banana", "cherry"];
$checkBanana = in_array("banana", $fruits); // $checkBanana = true
$numbers = [1, 2, 3, 4, 5];
$checkSix = in_array(6, $numbers); // $checkSix = false
$numbers = [1, 2, 3];
$checkStringTwo = in_array("2", $numbers, true);
// $checkStringTwo = false
array_key_exists($key, $array)
$fruits = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$checkKeyB = array_key_exists("b", $fruits); // $checkKeyB = true
$numbers = [1 => "one", 2 => "two", 3 => "three"];
$checkKeyFour = array_key_exists(4, $numbers); // $checkKeyFour = false
array_merge($array1, $array2, ...)
$array1 = ["a", "b", "c"];
$array2 = ["d", "e", "f"];
$mergedArray = array_merge($array1, $array2);
// $mergedArray = ["a", "b", "c", "d", "e", "f"]
array_slice($array, $offset, $length, $preserve_keys)
$fruits = ["apple", "banana", "peach", "mango", "kiwi"];
$slice1 = array_slice($fruits, 1, 2);
$slice2 = array_slice($fruits, 2);
$slice3 = array_slice($fruits, -2, 2);
// $slice1 = ["banana", "peach"]
// $slice2 = ["peach", "mango", "kiwi"]
// $slice3 = ["mango", "kiwi"]
array_search($value, $array, $strict)
$fruits = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$keyBanana = array_search("banana", $fruits);
$numbers = [1, 2, 3, 4, 5];
$keyFour = array_search(4, $numbers);
// $keyBanana = "b"
// $keyFour = 3
array_reverse($array, $preserve_keys)
$fruits = ["apple", "banana", "cherry"];
$reversedFruits = array_reverse($fruits);
// $reversedFruits = ["cherry", "banana", "apple"]
$fruitsAssoc = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$reversedFruitsAssoc = array_reverse($fruitsAssoc, true);
// $reversedFruitsAssoc = ["c" => "cherry", "b" => "banana", "a" => "apple"]
array_values($array)
$fruits = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$values = array_values($fruits);
// $values = ["apple", "banana", "cherry"]
array_combine($keys, $values)
$keys = ["a", "b", "c"];
$values = ["apple", "banana", "cherry"];
$combinedArray = array_combine($keys, $values);
// $combinedArray = ["a" => "apple", "b" => "banana", "c" => "cherry"]
array_push($array, $value1, $value2, ...)
$fruits = ["apple", "banana"];
array_push($fruits, "cherry", "peach");
// $fruits = ["apple", "banana", "cherry", "peach"]
array_unshift($array, $value1, $value2, ...)
$fruits = ["apple", "banana"];
array_unshift($fruits, "cherry", "peach");
// $fruits = ["cherry", "peach", "banana", "cherry"]
array_pop($array)
$fruits = ["apple", "banana", "cherry"];
$lastFruit = array_pop($fruits);
// $lastFruit = "cherry", $fruits = ["apple", "banana"]
array_shift($array)
$fruits = ["apple", "banana", "cherry"];
$firstFruit = array_shift($fruits);
// $firstFruit = "apple", $fruits = ["banana", "cherry"]
array_unique($array, $sort_flags)
$numbers = [1, 2, 2, 3, 4, 4, 5];
$uniqueNumbers = array_unique($numbers);
// $uniqueNumbers = [1, 2, 3, 4, 5]
辛苦觀看到這邊的你們~~see you tomorrow